domctl/vNUMA: avoid arithmetic overflow
authorJan Beulich <jbeulich@suse.com>
Thu, 6 Feb 2020 08:55:18 +0000 (09:55 +0100)
committerJan Beulich <jbeulich@suse.com>
Thu, 6 Feb 2020 08:55:18 +0000 (09:55 +0100)
commitb0dd772650e087cf474cd20abf23508b9b094f42
tree57d49ddcd248199fcf82a66eff76b13090dc140a
parent6531ace86e669a04da32fe770580355513a60712
domctl/vNUMA: avoid arithmetic overflow

Checking the result of a multiplication against a certain limit has no
sufficient implication on the original value's range. In the case here
it is in particular problematic that while handling the domctl we do

    if ( copy_from_guest(info->vdistance, uinfo->vdistance,
                         nr_vnodes * nr_vnodes) )
        goto vnuma_fail;

which means copying sizeof(unsigned int) * (nr_vnodes * nr_vnodes)
bytes, and the handling of XENMEM_get_vnumainfo similarly has

        tmp.vdistance = xmalloc_array(unsigned int, dom_vnodes * dom_vnodes);

which means allocating sizeof(unsigned int) * (dom_vnodes * dom_vnodes)
bytes, whereas in then goes on doing this:

        memcpy(tmp.vdistance, d->vnuma->vdistance,
               sizeof(*d->vnuma->vdistance) * dom_vnodes * dom_vnodes);

Note the lack of parentheses in the multiplication expression.

Adjust the overflow check, moving the must-not-be-zero one right next to
it to avoid questions on whether there might be division by zero.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Wei Liu <wl@xen.org>
xen/common/domctl.c